package com.cherry.aop.service.impl;

import com.cherry.aop.dao.IAccountDao;
import com.cherry.aop.domain.Account;
import com.cherry.aop.service.IAccountService;
import com.cherry.aop.util.TransactionManager;

/**
 * java基于接口实现的动态代理
 */
public class AccountServiceImpl1 implements IAccountService {

    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }


    public void transfer(String sourceName, String targetName, Double money) {
        //2.1根据名称查询转出账户
        Account source = accountDao.findAccountByName(sourceName);
        //2.2根据名称查询转入账户
        Account target = accountDao.findAccountByName(targetName);
        //2.3转出账户减钱
        source.setMoney(source.getMoney() - money);
        //2.4转入账户加钱
        target.setMoney(target.getMoney() + money);
        //2.5更新转出账户
        accountDao.updateAccount(source);

//            int i=1/0;

        //2.6更新转入账户
        accountDao.updateAccount(target);

    }
}
